<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(A卷,100分)- 处理器问题（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>某公司研发了一款高性能AI处理器。每台物理设备具备8颗<strong>AI</strong>处理器&#xff0c;编号分别为0、1、2、3、4、5、6、7。</p> 
<p>编号0-3的处理器处于同一个链路中&#xff0c;编号4-7的处理器处于另外一个链路中&#xff0c;不通链路中的处理器不能通信。</p> 
<p>如下图所示。现给定服务器可用的处理器编号数组array&#xff0c;以及任务申请的处理器数量num&#xff0c;找出符合下列亲和性调度原则的芯片组合。</p> 
<p>如果不存在符合要求的组合&#xff0c;则返回<strong>空列表</strong>。</p> 
<p></p> 
<p>亲和性调度原则&#xff1a;</p> 
<p>-如果申请处理器个数为1&#xff0c;则选择同一链路&#xff0c;剩余可用的处理器数量为1个的最佳&#xff0c;其次是剩余3个的为次佳&#xff0c;然后是剩余2个&#xff0c;最后是剩余4个。</p> 
<p>-如果申请处理器个数为2&#xff0c;则选择同一链路剩余可用的处理器数量2个的为最佳&#xff0c;其次是剩余4个&#xff0c;最后是剩余3个。</p> 
<p>-如果申请处理器个数为4&#xff0c;则必须选择同一链路剩余可用的处理器数量为4个。</p> 
<p>-如果申请处理器个数为8&#xff0c;则申请节点所有8个处理器。</p> 
<p></p> 
<p>提示&#xff1a;</p> 
<ol><li>任务申请的处理器数量只能是1、2、4、8。</li><li>编号0-3的处理器处于一个链路&#xff0c;编号4-7的处理器处于另外一个链路。</li><li>处理器编号唯一&#xff0c;且不存在相同编号处理器。</li></ol> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>输入包含可用的处理器编号数组array&#xff0c;以及任务申请的处理器数量num两个部分。</p> 
<p>第一行为array&#xff0c;第二行为num。例如&#xff1a;</p> 
<blockquote> 
 <p>[0, 1, 4, 5, 6, 7]<br /> 1</p> 
</blockquote> 
<p>表示当前编号为0、1、4、5、6、7的处理器可用。任务申请1个处理器。</p> 
<ul><li><code>0 &lt;&#61; array.length &lt;&#61; 8</code></li><li><code>0 &lt;&#61; array[i] &lt;&#61; 7</code></li><li><code>num in [1, 2, 4, 8]</code></li></ul> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出为组合列表&#xff0c;当array&#61;[0&#xff0c;1&#xff0c;4&#xff0c;5&#xff0c;6&#xff0c;7]&#xff0c;num&#61;1 时&#xff0c;输出为[[0], [1]]。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:64px;">输入</td><td style="width:434px;">[0, 1, 4, 5, 6, 7]<br /> 1</td></tr><tr><td style="width:64px;">输出</td><td style="width:434px;">[[0], [1]]</td></tr><tr><td style="width:64px;">说明</td><td style="width:434px;"> <p>根据第一条亲和性调度原则&#xff0c;在剩余两个处理器的链路&#xff08;0, 1, 2, 3&#xff09;中选择处理器。</p> <p>由于只有0和1可用&#xff0c;则返回任意一颗处理器即可。</p> </td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:63px;">输入</td><td style="width:435px;">[0, 1, 4, 5, 6, 7]<br /> 4</td></tr><tr><td style="width:63px;">输出</td><td style="width:435px;">[[4, 5, 6, 7]]</td></tr><tr><td style="width:63px;">说明</td><td style="width:435px;">根据第三条亲和性调度原则&#xff0c;必须选择同一链路剩余可用的处理器数量为4个的环</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>用例中&#xff0c;链路link1&#61;[0,1]&#xff0c;链路link2&#61;[4,5,6,7]</p> 
<p>现在要选1个处理器&#xff0c;则需要按照亲和性调度原则</p> 
<blockquote> 
 <p>如果申请处理器个数为1&#xff0c;则选择同一链路&#xff0c;剩余可用的处理器数量为1个的最佳&#xff0c;其次是剩余3个的为次佳&#xff0c;然后是剩余2个&#xff0c;最后是剩余4个。</p> 
</blockquote> 
<p>最佳的是&#xff0c;找剩余可用1个处理器的链路&#xff0c;发现没有&#xff0c;link1剩余可用2&#xff0c;link2剩余可用4</p> 
<p>其次的是&#xff0c;找剩余可用3个处理器的链路&#xff0c;发现没有</p> 
<p>再次的是&#xff0c;找剩余可用2个处理器的链路&#xff0c;link1符合要求&#xff0c;即从0和1处理器中任选一个&#xff0c;有两种选择&#xff0c;可以使用dfs找对应组合。</p> 
<p></p> 
<p>关于回溯算法求解组合&#xff0c;可以看下&#xff1a;</p> 
<p><a href="https://fcqian.blog.csdn.net/article/details/127440020" rel="nofollow" title="LeetCode - 77 组合_算法的组合_伏城之外的博客-CSDN博客">LeetCode - 77 组合_算法的组合_伏城之外的博客-CSDN博客</a></p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 2) {
    const arr &#61; JSON.parse(lines[0]);
    const num &#61; lines[1];

    console.log(getResult(arr, num));

    lines.length &#61; 0;
  }
});

function getResult(arr, num) {
  const link1 &#61; [];
  const link2 &#61; [];

  arr
    .sort((a, b) &#61;&gt; a - b)
    .forEach((e) &#61;&gt; {
      e &lt; 4 ? link1.push(e) : link2.push(e);
    });

  const ans &#61; [];
  const len1 &#61; link1.length;
  const len2 &#61; link2.length;

  switch (num) {
    case &#34;1&#34;:
      if (len1 &#61;&#61;&#61; 1 || len2 &#61;&#61;&#61; 1) {
        if (len1 &#61;&#61;&#61; 1) dfs(link1, 0, 1, [], ans);
        if (len2 &#61;&#61;&#61; 1) dfs(link2, 0, 1, [], ans);
      } else if (len1 &#61;&#61;&#61; 3 || len2 &#61;&#61;&#61; 3) {
        if (len1 &#61;&#61;&#61; 3) dfs(link1, 0, 1, [], ans);
        if (len2 &#61;&#61;&#61; 3) dfs(link2, 0, 1, [], ans);
      } else if (len1 &#61;&#61;&#61; 2 || len2 &#61;&#61;&#61; 2) {
        if (len1 &#61;&#61;&#61; 2) dfs(link1, 0, 1, [], ans);
        if (len2 &#61;&#61;&#61; 2) dfs(link2, 0, 1, [], ans);
      } else if (len1 &#61;&#61;&#61; 4 || len2 &#61;&#61;&#61; 4) {
        if (len1 &#61;&#61;&#61; 4) dfs(link1, 0, 1, [], ans);
        if (len2 &#61;&#61;&#61; 4) dfs(link2, 0, 1, [], ans);
      }
      break;
    case &#34;2&#34;:
      if (len1 &#61;&#61;&#61; 2 || len2 &#61;&#61;&#61; 2) {
        if (len1 &#61;&#61;&#61; 2) dfs(link1, 0, 2, [], ans);
        if (len2 &#61;&#61;&#61; 2) dfs(link2, 0, 2, [], ans);
      } else if (len1 &#61;&#61;&#61; 4 || len2 &#61;&#61;&#61; 4) {
        if (len1 &#61;&#61;&#61; 4) dfs(link1, 0, 2, [], ans);
        if (len2 &#61;&#61;&#61; 4) dfs(link2, 0, 2, [], ans);
      } else if (len1 &#61;&#61;&#61; 3 || len2 &#61;&#61;&#61; 3) {
        if (len1 &#61;&#61;&#61; 3) dfs(link1, 0, 2, [], ans);
        if (len2 &#61;&#61;&#61; 3) dfs(link2, 0, 2, [], ans);
      }
      break;
    case &#34;4&#34;:
      if (len1 &#61;&#61;&#61; 4 || len2 &#61;&#61;&#61; 4) {
        if (len1 &#61;&#61;&#61; 4) ans.push(link1);
        if (len2 &#61;&#61;&#61; 4) ans.push(link2);
      }
      break;
    case &#34;8&#34;:
      if (len1 &#61;&#61;&#61; 4 &amp;&amp; len2 &#61;&#61;&#61; 4) {
        ans.push([...link1, ...link2]);
      }
      break;
  }

  return JSON.stringify(ans).split(&#34;,&#34;).join(&#34;, &#34;);
}

function dfs(arr, index, level, path, res) {
  if (path.length &#61;&#61;&#61; level) {
    return res.push([...path]);
  }

  for (let i &#61; index; i &lt; arr.length; i&#43;&#43;) {
    path.push(arr[i]);
    dfs(arr, i &#43; 1, level, path, res);
    path.pop();
  }
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    Integer[] arr &#61;
        Arrays.stream(sc.nextLine().split(&#34;[\\[\\]\\,\\s]&#34;))
            .filter(str -&gt; !&#34;&#34;.equals(str))
            .map(Integer::parseInt)
            .toArray(Integer[]::new);

    String num &#61; sc.next();

    System.out.println(getResult(arr, num));
  }

  public static String getResult(Integer[] arr, String num) {
    ArrayList&lt;Integer&gt; link1 &#61; new ArrayList&lt;&gt;();
    ArrayList&lt;Integer&gt; link2 &#61; new ArrayList&lt;&gt;();

    Arrays.sort(arr, (a, b) -&gt; a - b);
    for (Integer e : arr) {
      if (e &lt; 4) {
        link1.add(e);
      } else {
        link2.add(e);
      }
    }

    ArrayList&lt;ArrayList&lt;Integer&gt;&gt; ans &#61; new ArrayList&lt;&gt;();
    int len1 &#61; link1.size();
    int len2 &#61; link2.size();

    switch (num) {
      case &#34;1&#34;:
        if (len1 &#61;&#61; 1 || len2 &#61;&#61; 1) {
          if (len1 &#61;&#61; 1) dfs(link1, 0, 1, new ArrayList&lt;&gt;(), ans);
          if (len2 &#61;&#61; 1) dfs(link2, 0, 1, new ArrayList&lt;&gt;(), ans);
        } else if (len1 &#61;&#61; 3 || len2 &#61;&#61; 3) {
          if (len1 &#61;&#61; 3) dfs(link1, 0, 1, new ArrayList&lt;&gt;(), ans);
          if (len2 &#61;&#61; 3) dfs(link2, 0, 1, new ArrayList&lt;&gt;(), ans);
        } else if (len1 &#61;&#61; 2 || len2 &#61;&#61; 2) {
          if (len1 &#61;&#61; 2) dfs(link1, 0, 1, new ArrayList&lt;&gt;(), ans);
          if (len2 &#61;&#61; 2) dfs(link2, 0, 1, new ArrayList&lt;&gt;(), ans);
        } else if (len1 &#61;&#61; 4 || len2 &#61;&#61; 4) {
          if (len1 &#61;&#61; 4) dfs(link1, 0, 1, new ArrayList&lt;&gt;(), ans);
          if (len2 &#61;&#61; 4) dfs(link2, 0, 1, new ArrayList&lt;&gt;(), ans);
        }
        break;
      case &#34;2&#34;:
        if (len1 &#61;&#61; 2 || len2 &#61;&#61; 2) {
          if (len1 &#61;&#61; 2) dfs(link1, 0, 2, new ArrayList&lt;&gt;(), ans);
          if (len2 &#61;&#61; 2) dfs(link2, 0, 2, new ArrayList&lt;&gt;(), ans);
        } else if (len1 &#61;&#61; 4 || len2 &#61;&#61; 4) {
          if (len1 &#61;&#61; 4) dfs(link1, 0, 2, new ArrayList&lt;&gt;(), ans);
          if (len2 &#61;&#61; 4) dfs(link2, 0, 2, new ArrayList&lt;&gt;(), ans);
        } else if (len1 &#61;&#61; 3 || len2 &#61;&#61; 3) {
          if (len1 &#61;&#61; 3) dfs(link1, 0, 2, new ArrayList&lt;&gt;(), ans);
          if (len2 &#61;&#61; 3) dfs(link2, 0, 2, new ArrayList&lt;&gt;(), ans);
        }
        break;
      case &#34;4&#34;:
        if (len1 &#61;&#61; 4 || len2 &#61;&#61; 4) {
          if (len1 &#61;&#61; 4) ans.add(link1);
          if (len2 &#61;&#61; 4) ans.add(link2);
        }
        break;
      case &#34;8&#34;:
        if (len1 &#61;&#61; 4 &amp;&amp; len2 &#61;&#61; 4) {
          ans.add(
              Stream.concat(link1.stream(), link2.stream())
                  .collect(Collectors.toCollection(ArrayList&lt;Integer&gt;::new)));
        }
        break;
    }

    return ans.toString();
  }

  public static void dfs(
      ArrayList&lt;Integer&gt; arr,
      int index,
      int level,
      ArrayList&lt;Integer&gt; path,
      ArrayList&lt;ArrayList&lt;Integer&gt;&gt; res) {
    if (path.size() &#61;&#61; level) {
      res.add(new ArrayList&lt;&gt;(path));
      return;
    }

    for (int i &#61; index; i &lt; arr.size(); i&#43;&#43;) {
      path.add(arr.get(i));
      dfs(arr, i &#43; 1, level, path, res);
      path.remove(path.size() - 1);
    }
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
arr &#61; eval(input())
num &#61; int(input())


# 算法入口
def getResult(arr, num):
    link1 &#61; []
    link2 &#61; []

    arr.sort()

    for e in arr:
        if e &lt; 4:
            link1.append(e)
        else:
            link2.append(e)

    ans &#61; []
    len1 &#61; len(link1)
    len2 &#61; len(link2)

    if num &#61;&#61; 1:
        if len1 &#61;&#61; 1 or len2 &#61;&#61; 1:
            if len1 &#61;&#61; 1:
                dfs(link1, 0, 1, [], ans)
            if len2 &#61;&#61; 1:
                dfs(link2, 0, 1, [], ans)
        elif len1 &#61;&#61; 3 or len2 &#61;&#61; 3:
            if len1 &#61;&#61; 3:
                dfs(link1, 0, 1, [], ans)
            if len2 &#61;&#61; 3:
                dfs(link2, 0, 1, [], ans)
        elif len1 &#61;&#61; 2 or len2 &#61;&#61; 2:
            if len1 &#61;&#61; 2:
                dfs(link1, 0, 1, [], ans)
            if len2 &#61;&#61; 2:
                dfs(link2, 0, 1, [], ans)
        elif len1 &#61;&#61; 4 or len2 &#61;&#61; 4:
            if len1 &#61;&#61; 4:
                dfs(link1, 0, 1, [], ans)
            if len2 &#61;&#61; 4:
                dfs(link2, 0, 1, [], ans)
    elif num &#61;&#61; 2:
        if len1 &#61;&#61; 2 or len2 &#61;&#61; 2:
            if len1 &#61;&#61; 2:
                dfs(link1, 0, 2, [], ans)
            if len2 &#61;&#61; 2:
                dfs(link2, 0, 2, [], ans)
        elif len1 &#61;&#61; 4 or len2 &#61;&#61; 4:
            if len1 &#61;&#61; 4:
                dfs(link1, 0, 2, [], ans)
            if len2 &#61;&#61; 4:
                dfs(link2, 0, 2, [], ans)
        elif len1 &#61;&#61; 3 or len2 &#61;&#61; 3:
            if len1 &#61;&#61; 3:
                dfs(link1, 0, 2, [], ans)
            if len2 &#61;&#61; 3:
                dfs(link2, 0, 2, [], ans)
    elif num &#61;&#61; 4:
        if len1 &#61;&#61; 4 or len2 &#61;&#61; 4:
            if len1 &#61;&#61; 4:
                ans.append(link1)
            if len2 &#61;&#61; 4:
                ans.append(link2)
    elif num &#61;&#61; 8:
        if len1 &#61;&#61; 4 and len2 &#61;&#61; 4:
            tmp &#61; []
            tmp.extend(link1)
            tmp.extend(link2)
            ans.append(tmp)

    return ans


def dfs(arr, index, level, path, res):
    if len(path) &#61;&#61; level:
        res.append(path[:])
        return

    for i in range(index, len(arr)):
        path.append(arr[i])
        dfs(arr, i &#43; 1, level, path, res)
        path.pop()


# 算法调用
print(getResult(arr, num))</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>